<?php

namespace app\common\model;

use think\Db;
use think\Model;

class Spec extends Model
{
    // 表名
    protected $name = 'spec';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    
    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text'
    ];
    

    



    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }
    
	public function category(){
		return $this->belongsTo('goodsCategory', 'category_id', 'id','', 'left')->setEagerlyType(0);
	}
	
	/**
	 * 添加规格值
	 * @param $specId
	 * @param $values
	 * @param int $type
	 * @return bool
	 */
	public function addItem($specId, $values, $type=1){
		if(!$values){
			return true;
		}
		
		$values = explode(';',str_replace('；',';',$values));
		//添加
		if($type==1){
			$datas = [];
			foreach ($values as $value){
				$datas[] = [
					'spec_id' => $specId,
					'name' => $value,
					'create_time' => time(),
					'update_time' => time(),
				];
			}
			
			if(Db::name('specItem')->insertAll($datas) === false){
				return false;
			}
		} else {
			//编辑
			$items = Db::name('specItem')->where(['spec_id' => $specId])->select();
			//已有的规格名称
			$oldItme = [];
			$delIds = [];
			if($items){
				foreach ($items as $item){
					if(!in_array($item['name'], $values)){
						$delIds[] = $item['id'];
					}
					$oldItme[] = $item['name'];
				}
			}
			
			//取新的差集
			$insertItme = array_diff($values,$oldItme);
			
			//要删除的
			$delItme = array_diff($oldItme, $values);
			//如果有新的，添加
			if($insertItme){
				$datas = [];
				foreach ($insertItme as $value){
					$datas[] = [
						'spec_id' => $specId,
						'name' => $value,
						'create_time' => time(),
						'update_time' => time(),
					];
				}
				
				if(Db::name('specItem')->insertAll($datas) === false){
					return false;
				}
			}
			
			if($delIds){
				$delRes = Db::name('specItem')->where(['id' => ['in',$delIds]])->delete();
				if($delRes === false){
					return false;
				}
			}
		}
		return true;
	}
	
	public function getInputArray($spec_arr, $title, $goodsId = ''){
	    // <input name="item[2_4_7][price]" value="100" /><input name="item[2_4_7][name]" value="蓝色_S_长袖" />
	    /*$spec_arr = array(
			20 => array('7','8','9'),
			10=>array('1','2'),
			1 => array('3','4'),
			
		);  */
	    // 排序
	    foreach ($spec_arr as $k => $v)
	    {
		    $spec_arr_sort[$k] = count($v);
	    }
	    asort($spec_arr_sort);
	    foreach ($spec_arr_sort as $key =>$val)
	    {
		    $spec_arr2[$key] = $spec_arr[$key];
		    $new_title[$key] = $title[$key];
	    }
	
	
	    $spec_arr2 = combineDika($spec_arr2); //  获取 规格的 笛卡尔积
	    
	    return ['title' => $new_title, 'items' => $spec_arr2];
    }
    
}
